package me.xinping.record.ui.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageButton
import android.widget.TextView
import me.xinping.record.R
import java.util.*

/**
 * .
 * <p>
 * Date 2018/12/4
 * @author fanxinping_cd@keruyun.com
 */

class RecorderAdapter(private val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val recorderItems: MutableList<RecorderItem> = arrayListOf()
    var isEditMode = false
    var playClickAction: (String) -> Unit = {}
    var itemCheckAction: (String, Boolean) -> Unit = { _, _ -> }

    /**
     * 编辑/取消编辑状态
     */
    fun edit(isEdit: Boolean = !isEditMode) {
        isEditMode = isEdit
        recorderItems.forEach { it.isEdit = isEdit }
        notifyDataSetChanged()
    }

    fun updateItems(items: List<RecorderItem>) {
        recorderItems.clear()
        recorderItems.addAll(items)
        notifyDataSetChanged()
    }

    fun findItemByFilePath(filePath: String?): RecorderItem? {
        return recorderItems.find { filePath == it.filePath }
    }

    fun updateItem(item: RecorderItem) {
        val index = recorderItems.indexOf(item)
        if (index >= 0) {
            recorderItems[index] = item
            notifyItemChanged(index)
        }
    }

    fun selectedItems(): List<RecorderItem> {
        return recorderItems.filter { it.isChecked }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
        ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_recorder, parent, false))

    override fun getItemCount(): Int = recorderItems.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as ViewHolder
        val item = recorderItems[position]
        vh.tvName.text = item.name
        vh.tvDuration.text = item.playProgress?.let { it } ?: item.duration
        vh.tvDate.text = item.date
        vh.cbCheck.visibility = if (item.isEdit) View.VISIBLE else View.GONE
        vh.cbCheck.isChecked = item.isChecked
        vh.cbCheck.setOnCheckedChangeListener { buttonView, isChecked ->
            buttonView.isPressed.let {
                item.isChecked = isChecked
                itemCheckAction.invoke(item.filePath, isChecked)
            }
        }
        vh.imbPlay.setImageResource(if (item.isPlaying) R.drawable.app_stop else R.drawable.app_play)
        vh.imbPlay.setOnClickListener { playClickAction.invoke(item.filePath) }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvName = itemView.findViewById<TextView>(R.id.tv_name)!!
        val tvDuration = itemView.findViewById<TextView>(R.id.tv_duration)!!
        val tvDate = itemView.findViewById<TextView>(R.id.tv_date)!!
        val imbPlay = itemView.findViewById<ImageButton>(R.id.imb_play)!!
        val cbCheck = itemView.findViewById<CheckBox>(R.id.cb_check)!!
    }

    data class RecorderItem(
        val name: String,
        val duration: String,
        val date: String,
        val filePath: String,
        var playProgress: String? = null,
        var isChecked: Boolean = false,
        var isPlaying: Boolean = false,
        var isEdit: Boolean = false
    ) {
        override fun equals(other: Any?): Boolean {
            return other is RecorderItem && other.filePath == filePath
        }

        override fun hashCode(): Int {
            return Objects.hash(name, date, filePath)
        }
    }

}